#!/bin/sh
#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
set +x
#@function: restre DB by rman.

SCRIPT_ROOT_PATH=$1
PID=$2
PARAM_NUM=$3
SCRIPTPID=$$


. "${SCRIPT_ROOT_PATH}/oracle_sbin_func.sh"
. "${SCRIPT_ROOT_PATH}/oracle_native_restore_func.sh"

#********************************define these for the functions of agent_sbin_func.sh********************************

# global var, for kill monitor
PARAM_CONTENT=`read_input_param "${PARAM_NUM}"`
test -z "$PARAM_CONTENT"              && exit_with_error "PARAM_FILE"

#############################################################
DBNAME=`get_value "${PARAM_CONTENT}" AppName`
DBINSTANCE=`get_value "${PARAM_CONTENT}" InstanceName`
DBUSER=`get_value "${PARAM_CONTENT}" UserName`
DBUSERPWD=`get_value "${PARAM_CONTENT}" Password`
ASMUSER=`get_value "${PARAM_CONTENT}" ASMUserName`
ASMUSERPWD=`get_value "${PARAM_CONTENT}" ASMPassword`
ASMSIDNAME=`get_value "${PARAM_CONTENT}" ASMInstanceName`
ORA_DB_USER=`get_value "${PARAM_CONTENT}" OracleInstallUser`
ORA_GRID_USER=`get_value "${PARAM_CONTENT}" GridInstallUser`
ORACLE_BASE=`get_value "${PARAM_CONTENT}" AccessOracleBase`
ORACLE_HOME=`get_value "${PARAM_CONTENT}" AccessOracleHome`

test "$DBNAME" = "ERROR_PARAM_INVALID"                       && exit_with_error "DBNAME"
test "$DBINSTANCE" = "ERROR_PARAM_INVALID"                   && exit_with_error "DBINSTANCE"
test "$DBUSER" = "ERROR_PARAM_INVALID"                       && exit_with_error "DBUSER"
test "$ASMUSER" = "ERROR_PARAM_INVALID"                      && exit_with_error "ASMUSER"
test "$ASMSIDNAME" = "ERROR_PARAM_INVALID"                   && exit_with_error "ASMSIDNAME"
# check parameter valid
test -z "$DBINSTANCE"  && exit_with_error "oracle instance name"

log_info "PID=${PID};DBINSTANCE=${DBINSTANCE};DBName=${DBNAME};DBUSER=${DBUSER};ASMInst=${ASMSIDNAME};ASMUSER=${ASMUSER};IN_ORACLE_HOME=$IN_ORACLE_HOME"

#********************************check host enviroment begin********************************
get_oracle_user

get_oracle_user_shell_type

env_file_list=`get_user_env_file "${ORA_DB_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_ORACLE_ENV=`generate_export_cmd "$env_file_list" "oracle"`
if [ $? -ne 0 ] || [ -z "$EXPORT_ORACLE_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi

check_oracle_is_install

check_oracle_install_type



if [ "$DBISCLUSTER" -gt 0 ]; then
    get_grid_user
    get_grid_user_shell_type
    get_grid_home_path
    env_file_list=`get_user_env_file "${ORA_GRID_USER}"`
    if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
        log_error "Get user env file failed"
        exit "$RETURN_INVALID"
    fi

    EXPORT_GRID_ENV=`generate_export_cmd "$env_file_list" "grid"`
    if [ $? -ne 0 ] || [ -z "$EXPORT_GRID_ENV" ]; then
        log_error "Get user env cmd failed"
        exit "$RETURN_INVALID"
    fi
    ASMSIDNAME=`ps -ef | grep asm_...._+ | grep -v grep | ${MYAWK} -F_ '{print $NF}' | uniq`
    if [ -z "$ASMSIDNAME" ]; then
        log_error "Have no asm instance"
        exit "${RETURN_INVALID}"
    fi
fi

get_oracle_base_path

get_oracle_home_path

get_oracle_version

check_sqlplus_status

check_rman_status

get_instance_auth_type "${DBINSTANCE}"
AUTH_TYPE=$?

#********************************prepare for restore begin********************************

stop_rac_instance()
{
    if [ ${AUTH_TYPE} -eq "$INSTANCE_LOGIN_OS_TYPE" ]; then
        su - "${ORA_DB_USER}" "${ORACLE_SHELL_MODE}" -c "${EXPORT_ORACLE_ENV} srvctl stop database -d ${DBNAME}" >> "${LOG_FILE_NAME}" 2>&1
        su - "${ORA_DB_USER}" "${ORACLE_SHELL_MODE}" -c "${EXPORT_ORACLE_ENV} srvctl remove database -d ${DBNAME} -f" >> "${LOG_FILE_NAME}" 2>&1
    else
        stop_single_instance "${DBINSTANCE}" "${DBUSER}" "${DBUSERPWD}"
    fi
}

# stop oracle database
if [ "${DBISCLUSTER}" -eq 1 ]; then
    stop_rac_instance
else
    stop_single_instance "${DBINSTANCE}" "${DBUSER}" "${DBUSERPWD}"
fi

if [ -f "${IN_ORACLE_HOME}/dbs/init${DBINSTANCE}.ora" ]; then
    su - "${ORA_DB_USER}" "${ORACLE_SHELL_MODE}" -c "${EXPORT_ORACLE_ENV} rm -rf ${IN_ORACLE_HOME}/dbs/init${DBINSTANCE}.ora"
fi
log_info "Do clivemountOK"
exit 0